package com.byron.pss.web.action;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.Random;
import java.util.UUID;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.text.StrBuilder;
import org.apache.struts2.ServletActionContext;

import com.byron.pss.dao.ILostFoundDAO;
import com.byron.pss.dao.IMessageDAO;
import com.byron.pss.dao.impl.LostFoundDAOImpl;
import com.byron.pss.dao.impl.MessageDAOImpl;
import com.byron.pss.domain.Const;
import com.byron.pss.domain.LostFoundStr;
import com.byron.pss.domain.Message;
import com.byron.pss.util.StringUtils;
import com.byron.pss.util.Transform;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

//主页内容，留言内容，验证码
public class IndexAction extends ActionSupport {

	private static final long serialVersionUID = -4897862602382071744L;

	private ILostFoundDAO dao = new LostFoundDAOImpl();
	private IMessageDAO msgDao = new MessageDAOImpl();

	private Message msg = new Message();
	private String code;// 验证码

	@Override
	public String execute() throws Exception {
		System.out.println("index ");
		// 封装要展示的信息
		List<LostFoundStr> list = Transform.list0jsp(dao.list());
		ActionContext.getContext().put("list", list);
		return super.execute();
	}

	@Override
	public String input() throws Exception {
		System.out.println("跳转到输入留言信息界面");
		return super.input();
	}

	// 对保存方法进行校验
	public void validateSave() {
		if (!StringUtils.hasLength(msg.getName())) {
			addFieldError("name", "姓名不能为空");
		}
		if (!StringUtils.hasLength(msg.getPhone())) {
			addFieldError("phone", "电话不能为空");
		}
		// 邮件没有进行正则判断
		if (!StringUtils.hasLength(msg.getEmail())) {
			addFieldError("email", "邮件不能为空");
		}
		// String randomCode = (String)
		// ActionContext.getContext().getSession().get(Const.RANDOMCODE_IN_SESSION);
		// if (!StringUtils.hasLength(randomCode)) {
		// addFieldError("code", "验证码为空");
		// }
		if (!Const.code.equals(code)) {
			addFieldError("code", "验证码错误");
		}
		System.out.println(code + "," + Const.code);
	}

	// 保存留言信息
	public String save() {
		msgDao.save(msg);
		return "save";
	}

	// 生成验证码图片
	public String code() throws IOException {
		HttpServletResponse resp = ServletActionContext.getResponse();
		// 生成随机数
		String randomCode = UUID.randomUUID().toString().substring(0, 5);

		Const.code = randomCode;
		// 把随机数放进Session中,未知原因，浏览器端保存的session信息是上一次的
		// ActionContext.getContext().getSession().put(Const.RANDOMCODE_IN_SESSION,
		// randomCode);
		// 创建图片对象
		int width = 80;
		int height = 40;
		int imageType = BufferedImage.TYPE_INT_RGB;
		BufferedImage image = new BufferedImage(width, height, imageType);

		// 画板
		Graphics g = image.getGraphics();
		g.setColor(Color.YELLOW);
		// 绘制一个实心的矩形
		g.fillRect(1, 1, width - 2, height - 2);

		// 把随机数画进图片中
		g.setColor(Color.BLACK);// 设置随机数的颜色
		Font font = new Font("宋体", Font.BOLD + Font.ITALIC, 20);
		g.setFont(font);// 设置随机数的字体和大小
		g.drawString(randomCode, 10, 28);
		// 干扰线
		g.setColor(Color.GRAY);
		Random r = new Random();
		for (int i = 0; i < 100; i++) {
			g.fillRect(r.nextInt(width), r.nextInt(height), 2, 2);
		}

		// 关闭
		g.dispose();
		// 把图片对象以流的方式保存出去
		ImageIO.write(image, "jpg", resp.getOutputStream());

		return NONE;
	}

	public Message getMsg() {
		System.out.println("api get msg");
		return msg;
	}

	public String getCode() {
		System.out.println("get code");
		return code;
	}

	public void setCode(String code) {
		System.out.println("set code");
		this.code = code;
	}
}
